package com.good.serviceImp;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import com.good.dao.BaseDao;
import com.good.entity.BaseEntity;
import com.good.service.BaseService;
import com.page.PageResults;

@Transactional
public class BaseServiceImpl<T, ID extends Serializable> implements
		BaseService<T, ID> {
	/** 更新忽略属性 */
	private static final String[] UPDATE_IGNORE_PROPERTIES = new String[] {
			BaseEntity.ID_PROPERTY_NAME, BaseEntity.CREATE_DATE_PROPERTY_NAME,
			BaseEntity.MODIFY_DATE_PROPERTY_NAME };
	/** baseDao */
	private BaseDao<T, ID> baseDao;

	public void setBaseDao(BaseDao<T, ID> baseDao) {
		this.baseDao = baseDao;
	}

	public void save(T entity) {
		baseDao.save(entity);
	}

	@Override
	public void saveOrUpdate(T t) {
		baseDao.saveOrUpdate(t);
	}

	@Override
	public T load(ID id) {
		return baseDao.load(id);
	}

	@Transactional(readOnly=true)
	@Override
	public T find(ID id) {
		return baseDao.find(id);
	}

	@Override
	public void delete(T entity) {
		baseDao.delete(entity);
	}

	@Override
	public boolean deleteById(ID id) {
		if (id != null) {
			return baseDao.deleteById(id);
		}
		return false;
	}

	@Override
	public void update(T entity) {
		baseDao.update(entity);
	}

	@Override
	public void refresh(T t) {
		baseDao.refresh(t);
	}

	@Override
	public void deleteAll(Collection<T> entities) {
		baseDao.deleteAll(entities);
	}
	
	@Transactional(readOnly=true)
	@Override
	public void queryHql(String hqlString, Object... values) {
		baseDao.queryHql(hqlString, values);
	}
	
	@Transactional(readOnly=true)
	@Override
	public void querySql(String sqlString, Object... values) {
		baseDao.querySql(sqlString, values);
	}
	@Transactional(readOnly=true)
	@Override
	public T getByHQL(String hqlString, Object... values) {

		return baseDao.findByHQL(hqlString, values);
	}
	@Transactional(readOnly=true)
	@Override
	public T getBySQL(String sqlString, Object... values) {
		return baseDao.findBySQL(sqlString, values);
	}
	@Transactional(readOnly=true)
	@Override
	public List<T> getListByHQL(String hqlString, Object... values) {
		return baseDao.findListByHQL(hqlString, values);
	}
	@Transactional(readOnly=true)
	@Override
	public List<T> getListBySQL(String sqlString, Object... values) {
		return baseDao.findListBySQL(sqlString, values);
	}
	@Transactional(readOnly=true)
	@Override
	public Long countByHql(String hql, Object... values) {
		return baseDao.findCount(hql, values);
	}
	@Transactional(readOnly=true)
	@Override
	public PageResults<T> findPageByFetchedHql(String hql, String countHql,
			int pageNo, int pageSize, Object... values) {
		return baseDao.findPage(hql, countHql, pageNo, pageSize, values);
	}

	@Override
	public void update(T entity, ID id, String... ignoreProperties) {
		T ignEntity = baseDao.load(id);
		BeanUtils.copyProperties(entity, ignEntity,
				addString(UPDATE_IGNORE_PROPERTIES, ignoreProperties));
	}

	private String[] addString(String[] value, String... values) {
		int valuesLen = values.length + 3;
		String[] str = new String[valuesLen];
		int valueLen = value.length;
		for (int i = 0; i < valueLen; i++) {
			str[i] = value[i];
		}
		for (int i = valueLen; i < values.length + valueLen; i++) {
			str[i] = values[i];
		}
		return str;
	}

}
